package Button
{
	import flash.display.Sprite;
	import flash.events.TextEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	public class UglyTextField extends Sprite
	{
		private var _textField : TextField = new TextField();
		private var _textFieldFormat : TextFormat = new TextFormat();				
		
		private var _labelVector : Vector.<TextField>;
		
		private var _charVector : Vector.<String>;
		
		private var _onlyOne : Boolean = false;
		
		private const MAX_CHARS : int = 8;
		
		public function UglyTextField(type:String, posX:Number, posY:Number)
		{
			super();
			
			//Create the vector
			_labelVector = new Vector.<TextField>();
			
			_charVector = new Vector.<String>();
			
			//Setting some native properties			
			_textFieldFormat.size = 17;
			_textFieldFormat.bold;
			_textFieldFormat.align = TextFormatAlign.CENTER;
			_textField.defaultTextFormat = _textFieldFormat;
			
			_textField.type = type; //INPUT OR DYNAMIC
			_textField.text = "";
			_textField.height = 20; //HARDCODE
			
			//Setting the Position
			this.x = posX;
			this.y = posY;					
			
			//Setting the background
			_textField.background = true;
			_textField.backgroundColor = 0x99FF99; //HARDCODE
			
			//Drawing a rect to use like a border
			with(this.graphics)
			{			
				beginFill(0x000000, 1);				
				drawRect(-2, -2, _textField.width + 4, _textField.height + 4); //HARCODE
				endFill();
			}
			
			//Add the TextField
			this.addChild(_textField);
			
			_textField.addEventListener(TextEvent.TEXT_INPUT, controlInput);
			//_textField.restrict = "0-9"
			
			//Limit the number of chars			
			_textField.maxChars = MAX_CHARS;
		}
		
		public function allowSymbol(symbol:String) : void
		{
			_charVector.push(symbol);
		}
		
		private function controlInput(evt:TextEvent) : void
		{
			var tmpBool : Boolean = false;
			
			if(evt.text == "0" || evt.text == "1" || evt.text == "2" || evt.text == "3" || evt.text == "4" || evt.text == "5" || evt.text == "6"
				|| evt.text == "7" || evt.text == "8" || evt.text == "9")
			{
				tmpBool = true;
			}
			else
			{
				for(var i:int = 0; i < _charVector.length; i++)
				{			
					if((_charVector[i] == evt.text && !tmpBool))
					{
						tmpBool = true;						
					}														
				}
			}			
			
			if(evt.text == "-" && tmpBool)
			{			
				if(_textField.caretIndex != 0)
				{
					evt.preventDefault();
				}
				
				for(var j:int = 0; j < _textField.text.length; j++)
				{
					if(_textField.text.charAt(j) == "-")
						evt.preventDefault();
				}
			}
			
			if(evt.text == "." && tmpBool)
			{			
				if(_textField.caretIndex == 0 || (_textField.text.charAt(0) == "-" && _textField.caretIndex == 1))
				{
					evt.preventDefault();
				}
				
				for(var k:int = 0; k < _textField.text.length; k++)
				{
					if(_textField.text.charAt(k) == ".")
						evt.preventDefault();
				}
			}
				
			if(!tmpBool)
				evt.preventDefault();	
		}
		
		public function addLabel(text:String, pos:String) : void
		{
			var tmpTextField : TextField = new TextField();
			tmpTextField.selectable = false; 
			tmpTextField.mouseEnabled = false;
			
			var tmpTextFormat : TextFormat = new TextFormat();			
			tmpTextFormat.size = 17;
			tmpTextFormat.bold;			
			tmpTextField.defaultTextFormat = tmpTextFormat;
			
			switch(pos)
			{
				case "Top":					
					tmpTextField.text = text;
					tmpTextField.x = 0;
					tmpTextField.y = (_textField.height + 5) * (-1);			
					break;
				case "Down":
					tmpTextField.text = text;
					tmpTextField.x = 0;
					tmpTextField.y = _textField.height + 5;
					break;
				case "Left":
					tmpTextField.text = text;
					tmpTextField.x = tmpTextField.text.length * 10 * (-1);
					tmpTextField.y = 0;
					break;
				case "Right":
					tmpTextField.text = text;
					tmpTextField.x = _textField.width + 5;
					tmpTextField.y = 0;
					break;
				default:
					tmpTextField.text = text;
					tmpTextField.x = _textField.width + 5;
					tmpTextField.y = 0;
					break;
			}
			
			addChild(tmpTextField);
			_labelVector.push(tmpTextField);
		}
		
		public function setPos(posX:Number, posY:Number) : void
		{
			this.x = posX;
			this.y = posY;					
		}
		
		public function getPos() : Point
		{
			return new Point(this.x, this.y);
		}
		
		public function setText(text:String) : void
		{
			if(text.length > MAX_CHARS)
				_textField.text = text.substring(0, MAX_CHARS);
			else
				_textField.text = text;
		}
		
		public function getTextAsNumber() : Number
		{
			return (Number)(_textField.text);
		}
		
		public function getTextAsString() : String
		{
			return _textField.text;
		}
	}
}